package com.hack.hackathon.service.impl;

import com.hack.hackathon.entity.Order;
import com.hack.hackathon.entity.OrderDetail;
import com.hack.hackathon.entity.User;
import com.hack.hackathon.entity.UserLog;
import com.hack.hackathon.mapper.OrderDetailMapper;
import com.hack.hackathon.mapper.OrderMapper;
import com.hack.hackathon.mapper.UserLogMapper;
import com.hack.hackathon.mapper.UserMapper;
import com.hack.hackathon.service.UserService;
import com.hack.hackathon.vo.OrderDetailVO;
import com.hack.hackathon.vo.PostDataVO;
import com.hack.hackathon.vo.TodayInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Wang Yong on 16-10-29.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserLogMapper userLogMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public boolean addIfNotExists(PostDataVO postDataVO){
        boolean result = false;
        if(postDataVO != null && StringUtils.isNotBlank(postDataVO.getPersonId())){
            if(userMapper.selectByPrimaryKey(postDataVO.getPersonId()) == null){
                User user = new User();
                user.setId(postDataVO.getPersonId());
                user.setAge(postDataVO.getAge().intValue());
                //user.setCreateTime(new Date());
                user.setCreateTime(new Date(postDataVO.getOrder().getCreateTime()));
                user.setFacesUrl(postDataVO.getUrl());
                user.setSexuality(postDataVO.getMale());
                user.setFeelHappy(postDataVO.getSmile().toString());
                user.setPeopleNum(postDataVO.getPersonCount());
                this.userMapper.insert(user);
                UserLog userLog = new UserLog();
                //userLog.setCreateTime(new Date());
                userLog.setCreateTime(new Date(postDataVO.getOrder().getCreateTime()));
                userLog.setOperation(1);
                userLog.setUserId(postDataVO.getPersonId());
                userLog.setRemark("新客户");
                this.userLogMapper.insert(userLog);

            }else{
                UserLog userLog = new UserLog();
                //userLog.setCreateTime(new Date());
                userLog.setCreateTime(new Date(postDataVO.getOrder().getCreateTime()));
                userLog.setOperation(2);
                userLog.setUserId(postDataVO.getPersonId());
                userLog.setRemark("老用户下单,订单编号:"+postDataVO.getOrder().getOrderNumber());
                this.userLogMapper.insert(userLog);
            }
            result = true;
        }
        return  result;
    }

    @Override
    @Transactional
    public boolean addOrder(PostDataVO postDataVO){
        if(postDataVO != null && StringUtils.isNotBlank(postDataVO.getPersonId()) &&
                StringUtils.isNotBlank(postDataVO.getOrder().getOrderNumber())){
            Order order = new Order();
            order.setUserId(postDataVO.getPersonId());
            order.setCreateTime(new Date(postDataVO.getOrder().getCreateTime()));
            order.setOrderNumber(postDataVO.getOrder().getOrderNumber());
            order.setTotalPrice(postDataVO.getOrder().getTotalPrice());
            this.orderMapper.insert(order);
            //System.out.println("add Order id"+order.getId());
            List<OrderDetailVO> orderDetailVOs = postDataVO.getOrder().getDetail();
            if(orderDetailVOs != null && !orderDetailVOs.isEmpty()){
                OrderDetail orderDetail;
                for(OrderDetailVO detailVO : orderDetailVOs){
                    orderDetail = new OrderDetail();
                    orderDetail.setOrderId(order.getId());
                    orderDetail.setProductCount(detailVO.getCount());
                    orderDetail.setProductName(detailVO.getName());
                    orderDetail.setProductNum(detailVO.getId());
                    orderDetail.setProductPrice(detailVO.getPrice());
                    this.orderDetailMapper.insert(orderDetail);
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public TodayInfoVO fetchTodayInfo(){
        TodayInfoVO infoVO = new TodayInfoVO();
        infoVO.setTodayIncome(orderMapper.selectTodayIncome());
        infoVO.setTodayVIPCount(userLogMapper.selectTodayBuyUserCount());
        infoVO.setTotalVIPCount(userMapper.allUserCount());
        return infoVO;
    }


}
